using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a list of int numbers.
    /// </summary>
    [Serializable]
    public class IntNumberCollection : AbstractCollection
    {
        /// <summary>
        /// The list for saving the numbers.
        /// </summary>
        private readonly List<int> numberList;

        /// <summary>
        /// Initializes a new instance of the <see cref="IntNumberCollection"/> class.
        /// </summary>
        public IntNumberCollection()
        {
            numberList = new List<int>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IntNumberCollection"/> class.
        /// </summary>
        /// <param name="numbers">The numbers in form of an int array.</param>
        public IntNumberCollection(int[] numbers)
        {
            if (numbers == (int[]) ((object) null))
            {
                throw new ArgumentNullException("numbers");
            }

            numberList = new List<int>();

            foreach (int data in numbers)
            {
                this.numberList.Add(data);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IntNumberCollection"/> class.
        /// </summary>
        /// <param name="numbers">The numbers in form of a list.</param>
        public IntNumberCollection(List<int> numbers)
        {
            if (numbers == (List<int>) null)
            {
                throw new ArgumentNullException("numbers");
            }

            numberList = new List<int>();

            foreach (int data in numbers)
            {
                this.numberList.Add(data);
            }
        }

        /// <summary>
        /// Gets the number list.
        /// </summary>
        /// <value>The number list.</value>
        public List<int> Value
        {
            get { return numberList; }
        }

        /// <summary>
        /// Count the current number of items in the list.
        /// </summary>
        /// <value>The current number of items in the list.</value>
        public int Count
        {
            get { return numberList.Count; }
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public IntNumberCollection Copy()
        {
            return new IntNumberCollection(this.ToArray());
        }

        /// <summary>
        /// Gets the number at a specified position.
        /// </summary>
        /// <param name="index">The specified position.</param>
        /// <returns>The number at the specified position.</returns>
        public int GetElementAt(int index)
        {
            return this.numberList[index];
        }

        /// <summary>
        /// Sets the number at a specified position.
        /// </summary>
        /// <param name="index">The index where the value should be set.</param>
        /// <param name="value">The value to set.</param>
        public void SetNumberAt(int index, int value)
        {
            this.numberList[index] = value;
        }

        /// <summary>
        /// Inserts the number at a specified position.
        /// </summary>
        /// <param name="index">The index where the value should be inserted.</param>
        /// <param name="value">The value to insert.</param>
        public void Insert(int index, int value)
        {
            this.numberList.Insert(index, value);
        }

        /// <summary>
        /// Removes the value at a specified position.
        /// </summary>
        /// <param name="index">The specified position where the item should be deleted.</param>
        public void RemoveAt(int index)
        {
            this.numberList.Remove(index);
        }

        /// <summary>
        /// Removes all numbers from the current list.
        /// </summary>
        public void Clear()
        {
            this.numberList.Clear();
        }

        /// <summary>
        /// Adds the number to the current list.
        /// </summary>
        /// <param name="value">The value to add.</param>
        public void Add(int value)
        {
            this.numberList.Add(value);
        }

        /// <summary>
        /// Sorts this list of numbers.
        /// </summary>
        public void Sort()
        {
            this.numberList.Sort();
        }

        /// <summary>
        /// Compares the current instance to another.
        /// </summary>
        /// <param name="compare">The collection to compare.</param>
        /// <returns>True if the collection values and the order of the items are even otherwise, false.</returns>
        public bool CompareTo(IntNumberCollection compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Converts the current list to an array.
        /// </summary>
        /// <returns>The converted int array.</returns>
        public int[] ToArray()
        {
            return this.numberList.ToArray();
        }

        /// <summary>
        /// Takes the square root of all items in the list.
        /// </summary>
        /// <returns>The calculated values in form of a new list.</returns>
        public IntNumberCollection Sqrt()
        {
            IntNumberCollection result = new IntNumberCollection();

            foreach (int data in this.numberList)
            {
                result.Add((int) Math.Sqrt(data));
            }

            return result;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(IntNumberCollection a, IntNumberCollection b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            if (a.Count != b.Count)
            {
                return false;
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (a.GetElementAt(i) != b.GetElementAt(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(IntNumberCollection a, IntNumberCollection b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator +(IntNumberCollection a, int b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            IntNumberCollection result = new IntNumberCollection();

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) + b);
            }

            return result;
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator +(IntNumberCollection a, IntNumberCollection b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("b");
            }

            IntNumberCollection result = new IntNumberCollection();

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("The number of items has to be even.");
            }

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) + b.GetElementAt(i));
            }

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator -(IntNumberCollection a, int b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            IntNumberCollection result = new IntNumberCollection();

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) - b);
            }

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator -(IntNumberCollection a, IntNumberCollection b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("b");
            }

            IntNumberCollection result = new IntNumberCollection();

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("The number of items has to be even.");
            }

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) - b.GetElementAt(i));
            }

            return result;
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator *(IntNumberCollection a, int b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            IntNumberCollection result = new IntNumberCollection();

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) * b);
            }

            return result;
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator *(IntNumberCollection a, IntNumberCollection b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("b");
            }

            IntNumberCollection result = new IntNumberCollection();

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("The number of items has to be even.");
            }

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) * b.GetElementAt(i));
            }

            return result;
        }

        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator /(IntNumberCollection a, int b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            IntNumberCollection result = new IntNumberCollection();

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) / b);
            }

            return result;
        }

        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="a">The first IntNumberCollection.</param>
        /// <param name="b">The second IntNumberCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static IntNumberCollection operator /(IntNumberCollection a, IntNumberCollection b)
        {
            if (a == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (IntNumberCollection) null)
            {
                throw new ArgumentNullException("b");
            }

            IntNumberCollection result = new IntNumberCollection();

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("The number of items has to be even.");
            }

            for (int i = 0; i < a.Count; i++)
            {
                result.SetNumberAt(i, a.GetElementAt(i) / b.GetElementAt(i));
            }

            return result;
        }

        /// <summary>
        /// Gets or sets the <see cref="System.Int32"/> at the specified index.
        /// </summary>
        /// <value>The numeric value.</value>
        public int this[int index]
        {
            get { return this.numberList[index]; }
            set { this.numberList[index] = value; }
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>The enumerator of the current iteration.</returns>
        public IEnumerator<int> GetEnumerator()
        {
            for (int i = 0; i < this.numberList.Count; i++)
            {
                yield return this.numberList[i];
            }
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "";

            foreach (int data in this.numberList)
            {
                result += data.ToString() + ";";
            }

            return result;
        }
    }
}